/*
2011.6.22
hujunjie
*/

$(document).ready(function() {
    $.get("/login/my_v3.php", {}, function(result) {
        var arr = $.parseJSON(result);
        if (!arr.length) return;
        //eval("var arr = " + result + "");
        switch (arr[0]) {
            case 1:
                // ÒÑµÇÂ¼
                $('.nolog').hide();
                $('.loged').find('.username').html(arr[1]);
                $('.loged').find('.logout').attr('href', arr[2]);
                $('.loged').show();
                break;
            case 0:
                $('.loged').hide();
                $('.nolog').show();
                break;
            default:
                break;
        }
    });
});

function loadjsfile(fileName) {
    var fileref = parent.document.createElement('script');
    fileref.setAttribute('type', 'text/javascript');
    fileref.setAttribute('src', fileName);
    if (typeof fileref != 'undefined') {
        parent.document.getElementsByTagName('head')[0].appendChild(fileref);
    }
}

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
    )
}
var initlogin = function() {
    var verb;
    if (typeof ver == "undefined") {
        var verb = "";
    } else {
        verb = ver;
    }
    $.ajax({
        url: '/login/login2.php?tab=&ver=' + verb,
        type: 'GET',
        dataType: 'json',
        success: function(arr) {
            switch (arr[0]) {
                case 1:
                    $.getScript(arr[1]).done(function() {
                        $('.logina').ztlogin({
                            onClick: function(ztmini, e) {
                                e.preventDefault();
                                var ajaxURL, jumpURL = $(ztmini.selector.target).attr('jumpurl');
                                if (jumpURL) {
                                    ajaxURL = '/login/login2.php?t=userdetail&jumpurl=' + jumpURL;
                                } else {
                                    ajaxURL = '/login/login2.php?tab=';
                                }
                                $.ajax({
                                    url: ajaxURL,
                                    type: 'GET',
                                    dataType: 'json',
                                    success: function(res) {
                                        if (res[0] == 1) {
                                            ztmini.show();
                                        } else {
                                            if (jumpURL) {
                                                window.location.href = jumpURL;
                                            } else {
                                                window.location.reload();
                                            }
                                            ztmini.hide();
                                        }
                                    }
                                })
                            },
                            onLoginSuccess: function(ztmini, userinfo) {
                                var jumpURL = $(ztmini.selector.target).attr('jumpurl');
                                if (jumpURL) {
                                    window.location.href = jumpURL;
                                } else {
                                    window.location.reload();
                                }
                            }
                        });
                    });
                    break;
                default:
                    $('.logina').click(function() {
                        var jumpURL = $(this).attr('jumpurl');
                        if (jumpURL) {
                            window.location.href = jumpURL;
                        } else {
                            window.location.reload();
                        }
                    })
                    break;
            }
        }
    })
};
initlogin();

function GetUrlString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //var r = window.top.location.search.substr(1).match(reg);
    var r = self.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function resizeShowDivIndex(id, mask) {
    var xScroll, yScroll;
    if (window.parent.innerHeight && window.parent.scrollMaxY) {
        xScroll = window.parent.innerWidth + window.parent.scrollMaxX;
        yScroll = window.parent.innerHeight + window.parent.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari

        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    windowHeight = $(window.parent).height();
    windowWidth = $(window.parent).width();
    var t = (windowHeight - parseInt($(id).find("div:first").height() + 100)) / 2;
    var l = (windowWidth - parseInt($(id).find("div:first").width())) / 2;

    $(mask).css({
        height: (yScroll > windowHeight) ? yScroll : windowHeight,
        width: (xScroll > windowWidth) ? xScroll : windowWidth
    });

    $(id).find("div:first").css({
        top: t,
        left: l
    });

}

function changeimg(acctype, v) {
    var hasElement = document.getElementById("codeimg_" + acctype) || document.getElementById("codeimg");
    if (!hasElement) {
        return false;
    }
    if (v) {
        v = '_' + v;
    } else {
        v = '';
    }
    if (acctype) {
        if (document.getElementById("codeimg_" + acctype) != null && typeof(document.getElementById("codeimg_" + acctype)) != 'undefined') {
            document.getElementById("codeimg_" + acctype).src = "img" + v + ".php?acctype=" + acctype + "&" + Math.random();
        }
    } else {
        document.getElementById("codeimg").src = "img" + v + ".php?" + Math.random();
    }
}